package com.it.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/*

 */
public class NativeJdbcTest {
    @Test
    public void test1() throws Exception {
//        1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//        2.获取连接对象
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc", "root", "root");
//        3.通过Connection连接对象获取预编译对象
        PreparedStatement pstm = con.prepareStatement("select * from account where id=?");
//        4.预编译对象注入参数，执行查询
        pstm.setInt(1,2);
        ResultSet rs = pstm.executeQuery();
//        5.解析结果集，获取查询数据
        while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString("name");
            float money = rs.getFloat("money");
            System.out.println(id+"---"+name+"---"+money);
        }
//        6.释放资源关闭连接
        rs.close();
        pstm.close();
        con.close();
    }
}
